/**
 * 上海易雍健康信息咨询有限公司<br/>
 * Health Cross Administrators Ltd.<br/>
 * 项目 his-platform<br/>
 * 创建时间：2018/6/6 14:05<br/>
 *
 * @author $Li YuZhe$
 */
package Util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.log4j.Logger;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

/**
 * 描述：<br/>
 * 类全名： com.hca.dataexg.his.utils.HttpUtil<br/>
 * 创建时间：2018/6/6 14:05<br/>
 * <p>
 * 修改时间：2018/6/6 14:05<br/>
 * 修改原因：#Mantis Issue Number或系统改修表编号#<br/>
 * 修改位置：#方法名或行数#<br/>
 *
 * @author $Li YuZhe$
 */
public class HttpUtil {

    private static Logger logger = Logger.getLogger(HttpUtil.class);
    private static Map<String, String> listMap;

    public static String doHttp(String url, String request) throws MalformedURLException {
        URL httpUrl = new URL(url);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        RequestConfig defaultRequestConfig = RequestConfig.custom()
                .setSocketTimeout(300000)
                .setConnectTimeout(300000)
                .setConnectionRequestTimeout(300000)
                .setStaleConnectionCheckEnabled(true)
                .build();
        HttpPost hp = new HttpPost(httpUrl.toString());
        hp.setConfig(defaultRequestConfig);
        StringEntity se = new StringEntity(request, ContentType.create("text/plain", "UTF-8"));
        hp.setEntity(se);
        String resStr = "";
        DataInputStream dis = null;
        InputStream is = null;
        try (CloseableHttpResponse response = httpClient.execute(hp)) {
            logger.info("response status:" + " " + response.getStatusLine());

            HttpEntity hre = response.getEntity();
            System.out.println("response entity: " + hre);
            is = hre.getContent();
            dis = new DataInputStream(is);
            byte[] resOri = new byte[(int) hre.getContentLength()];
            dis.readFully(resOri);
            resStr = new String(resOri, "UTF-8");
            return resStr;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                dis.close();
                is.close();
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resStr;
    }

}